package com.cremamobile.filemanager;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CremaDialog extends Dialog {
	public static final int BUTTON_OK = 100;
	public static final int BUTTON_CANCEL = 101;
	
	// TODO. | 연산자를 통해 복수개의 타입을 선택할 수 있도록 수정 예정 
	// default  button 2개, 안내문구 text 가 보이도록. 
	public static final int TYPE_ALERT_BUTTON_1 = 1;
	public static final int TYPE_ALERT_BUTTON_2 = 2;
	public static final int TYPE_IMAGE = 4;
	public static final int TYPE_INPUT = 8;
	public static final int TYPE_SELECT_COPY = 16;
	public static final int TYPE_SELECT_MOVE = 32;
	public static final int TYPE_SELECT_SORT = 64;
	public static final int TYPE_ABOUT = 128;
	
	private View mLayout;
    private Button mConfirmButton;
    private Button mCancelButton;
    private ImageView mImageView;
    private EditText mTextField;
    private String mTitle;
    private String mContent;
    
    private Context mContext;
    private boolean donworkHandler = false;
    
//    private View.OnClickListener mLeftClickListener;
//    private View.OnClickListener mRightClickListener;
    private View.OnClickListener mbtnClickListener;
    private View.OnClickListener mSelectClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if ( ! donworkHandler) {
				dismiss();
				if (mbtnClickListener != null) {
					mbtnClickListener.onClick(v);
				}
			}
		}
    	
    };
    
    private int type = 0;
    
    public CremaDialog(Context context, int type, String title, String message, View.OnClickListener listener) {
    	this(context);
    	this.type = type;
    	this.mTitle = title;
    	this.mContent = message;
    	this.mbtnClickListener = listener;
    	createDialogByType(type);
    }

    public CremaDialog(Context context, int type, View.OnClickListener listener) {
    	this(context);
    	this.type = type;
    	this.mbtnClickListener = listener;
    	createDialogByType(type);
    }

    public CremaDialog(Context context, int type, String title, String message) {
    	this(context);
    	this.type = type;
    	this.mTitle = title;
    	this.mContent = message;
    	createDialogByType(type);
    }

    public CremaDialog(Context context, int type) {
    	this(context);
    	this.type = type;
    	
    	createDialogByType(type);
    }

    public CremaDialog(Context context) {
		super(context, R.style.CremaDialog);
    	this.mContext = context;
    }
    
    public void setClickListener(View.OnClickListener listener) {
    	this.mbtnClickListener = listener;
    	if (mConfirmButton != null)
    		mConfirmButton.setOnClickListener(listener);
    	if (mCancelButton != null)
    		mCancelButton.setOnClickListener(listener);
    }

    private void createDialogByType(int type) {
    	switch(type) {
    	case TYPE_ABOUT:
    		createAboutDialog();
    		break;
    	case TYPE_SELECT_COPY:
    	case TYPE_SELECT_MOVE:
    	case TYPE_SELECT_SORT:
    		createSelectDialog();
    		break;
    	default:
    		createAlertDialog();
    		break;
    	}
    }
    
    private void createAboutDialog() {
    	setContentView(R.layout.about_dialog);
        Button closeButton = (Button) findViewById(R.id.button_confirm);
        closeButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int id = v.getId();
				if (id == R.id.button_confirm) {
					dismiss();
				}
			}

        });
    }

    private void createSelectDialog() {
    	setContentView(R.layout.select_dialog);
        
        if (type == TYPE_SELECT_COPY || type == TYPE_SELECT_MOVE) {
            TextView titleText = (TextView) findViewById(R.id.dialog_title_text);
            titleText.setText(R.string.alert);

        	LinearLayout lv = (LinearLayout) findViewById(R.id.body_select_file_work_type);
        	lv.setVisibility(View.VISIBLE);
        	
            TextView messageView = (TextView) lv.findViewById(R.id.dialog_message);
            
            String text = null;
            if (type == TYPE_SELECT_COPY)
            	text = mContext.getResources().getString(R.string.copy_alert_message);
            else 
            	text = mContext.getResources().getString(R.string.move_alert_message);
            messageView.setText(Html.fromHtml(text));
            
        	
        	Button btn = (Button) lv.findViewById(R.id.button_all_file_replace);
        	btn.setText(R.string.all_file_replace);
        	btn.setOnClickListener(mSelectClickListener);
        	
        	btn = (Button) lv.findViewById(R.id.button_rename);
        	btn.setText(R.string.copy_rename);
        	btn.setOnClickListener(mSelectClickListener);
        	
        	btn = (Button) lv.findViewById(R.id.button_cancel);
        	if (type == TYPE_SELECT_COPY)
        		btn.setText(R.string.copy_cancel);
        	else 
        		btn.setText(R.string.move_cancel);
        	btn.setOnClickListener(mSelectClickListener);
        	
        } else if (type == TYPE_SELECT_SORT){
        	// 현재  sort 상태를 focus로 줘야 함.  focus가 호출되면서  click 효과를 주기 때문에
        	//  focus를 줄 때의 click 을 막기 위함.
        	donworkHandler = true;
        	
            TextView titleText = (TextView) findViewById(R.id.dialog_title_text);
            titleText.setText(R.string.sort);

        	LinearLayout lv = (LinearLayout) findViewById(R.id.body_select_sort_type);
        	lv.setVisibility(View.VISIBLE);
        	
        	Button btn = (Button) lv.findViewById(R.id.button_sortby_name);
        	btn.setOnClickListener(mSelectClickListener);
        	btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        v.performClick();
                    }
                }
            });
        	
        	btn = (Button) lv.findViewById(R.id.button_sortby_filetype);
        	btn.setOnClickListener(mSelectClickListener);
        	btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        v.performClick();
                    }
                }
            });
        	
        	btn = (Button) lv.findViewById(R.id.button_sortby_date);
        	btn.setOnClickListener(mSelectClickListener);
        	btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        v.performClick();
                    }
                }
            });
        	
        	btn = (Button) lv.findViewById(R.id.button_sortby_size);
        	btn.setOnClickListener(mSelectClickListener);
        	btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        v.performClick();
                    }
                }
            });
        }
        
        Button closeButton = (Button) findViewById(R.id.dialog_title_close_button);
        closeButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int id = v.getId();
				if (id == R.id.button_confirm) {
					dismiss();
				}
			}

        });
        if (type == TYPE_SELECT_SORT) {
        	closeButton.setVisibility(View.GONE);
        }
    }

    private void createAlertDialog() {
    	setContentView(R.layout.default_alert_dialog);
        
        TextView titleText = (TextView) findViewById(R.id.dialog_title_text);
        if (mTitle != null)
        	titleText.setText(mTitle);
        
	    LinearLayout lv = (LinearLayout) findViewById(R.id.dialog_body);
	    mImageView = (ImageView) lv.findViewById(R.id.dialog_image);
	    if (type == TYPE_IMAGE) {
	    	mImageView.setVisibility(View.VISIBLE);
	    }
        TextView contentView = (TextView) lv.findViewById(R.id.dialog_message);
        if (mContent != null)
        	contentView.setText(mContent);
        
        LinearLayout inputLayout = (LinearLayout) lv.findViewById(R.id.dialog_input);
        mTextField = (EditText) inputLayout.findViewById(R.id.dialog_edit);
        mTextField.setHorizontallyScrolling(true);
        
        Button textfieldRemove = (Button) inputLayout.findViewById(R.id.dialog_edit_remove);
        textfieldRemove.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mTextField.setText("");
			}
        });

        if (type == TYPE_INPUT) {
        	Window win = getWindow();
        	WindowManager.LayoutParams winLp = win.getAttributes();
        	winLp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
        	win.setAttributes(winLp);

        	inputLayout.setVisibility(View.VISIBLE);
        }
        
        mConfirmButton = (Button) lv.findViewById(R.id.button_confirm);
        mConfirmButton.setOnClickListener(mbtnClickListener);
        mCancelButton = (Button) lv.findViewById(R.id.button_cancel);
        mCancelButton.setOnClickListener(mbtnClickListener);

        if (type == TYPE_ALERT_BUTTON_1 || type == TYPE_IMAGE)
        	mConfirmButton.setVisibility(View.VISIBLE);
        else {
        	mConfirmButton.setVisibility(View.VISIBLE);
        	mCancelButton.setVisibility(View.VISIBLE);
        }
    }
    
    public void setImage(int res) {
    	mImageView.setImageResource(res);
    }
    
    public String getEditText() {
    	if (mTextField != null) {
    		return mTextField.getText().toString();
    	}
    	return null;
    }
    
    public void setEditText(String text) {
    	if (mTextField != null) {
    		mTextField.setText(text);
    	}
    	
    }
    
    public void focusView(int resId) {
    	View view = findViewById(resId);
    	if (view != null) {
    		view.requestFocus();
    		// 한번 포커스가 표시되었으면 그 이후에는 사용자의  Click을 처리하기 위해 false로 세팅함
    		donworkHandler = false;
    	}
    }
}
